﻿using Mapster;
using MapsterMapper;

namespace Framework.Core.ObjectMapper.Mapster
{
	public static class MapsterExtension
	{
		/// <summary>
		/// Mapster配置
		/// </summary>
		/// <param name="serviceCollection"></param>
		/// <param name="assemblyList"></param>
		public static void AddMapster(this IServiceCollection serviceCollection)
		{
			// 获取全局映射配置
			var config = TypeAdapterConfig.GlobalSettings;

			if (AssemblyUtil.GetAssemblyList().ToList() == null) return;
			var assemblies = AssemblyUtil.GetAssemblyList().ToArray();

			// 扫描所有继承  IRegister 接口的对象映射配置
			if (assemblies != null && assemblies.Length > 0) config.Scan(assemblies);

			// 配置默认全局映射（支持覆盖）
			config.Default
				  .NameMatchingStrategy(NameMatchingStrategy.Flexible)
				  .PreserveReference(true);

			// 配置默认全局映射（忽略大小写敏感）
			config.Default
				  .NameMatchingStrategy(NameMatchingStrategy.IgnoreCase)
				  .PreserveReference(true);

			// 配置支持依赖注入
			serviceCollection.AddSingleton(config);
			serviceCollection.AddScoped<IMapper, ServiceMapper>();
		}
	}
}
